﻿using MeasureAbstract.Base;
using MeasureAbstract.IMotionDevice;
using MeasureAbstract.IVectorNetworkAnalyzer;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Reflection;
using System.IO;
using MeasureAbstract.ISignalGenerator;
using System.Threading;
using System.Windows;
using MeasureAbstract.IFarfieldMeasure;

namespace MeasureLibrary.FarfieldMeasure
{
    public sealed class FarfieldMeasure_VSM : IFarfieldMeasure
    {
        public FarfieldMeasure_VSM()
        {
            if (DeviceList == null)
            {
                DeviceList = new ObservableCollection<IDeviceBase>();
            }
            DeviceList.Clear();
            DeviceList.Add(null); //添加一个转台
            DeviceList.Add(null); //添加一个矢网
            DeviceList.Add(null); //添加一个信号源

        }


        public override string MeasureName => "远场测试";

        public override string Description => "常见的测试架构，使用一个转台、一台矢网和一个信号发生器完成测试。";

        public double process = 0;
        public override double Process
        {
            get
            {
                return process;
            }
            set
            {
                SetProperty(ref process, value);
            }
        }
        public override async Task MeasureInitialize(object obj)
        {
            await Task.Run(() => 
            {
                InitBreak = false;
                try
                {
                    var VNA = DeviceList[1] as IVectorNetworkAnalyzer; //矢网
                    var MC = DeviceList[0] as IMotionDevice; //转台
                    var SG = DeviceList[2] as ISignalGenerator; //信号源

                    RealTimeCache.Completed();
                    Thread.Sleep(1000); //等待缓存关闭
                    Process = 0;

                    //数据初始化
                    if (DataDictFrequency_PositionCollection == null)
                    {
                        DataDictFrequency_PositionCollection = new Dictionary<double, ObservableCollection<DataItem>>();
                    }
                    else
                    {
                        DataDictFrequency_PositionCollection.Clear();
                    }
                    if (DataDictPosition_FrequencyCollection == null)
                    {
                        DataDictPosition_FrequencyCollection = new Dictionary<double, ObservableCollection<DataItem>>();
                    }
                    else
                    {
                        DataDictPosition_FrequencyCollection.Clear();
                    }
                    StopFlag = false;
                    ManualResetEvent.Set();//取消阻塞


                    //验证存储文件名
                    if (!System.IO.Directory.Exists(SystemConfig.DataDirectory))
                    {
                        try
                        {
                            System.IO.Directory.CreateDirectory(SystemConfig.DataDirectory);
                        }
                        catch (Exception e)
                        {

                            throw new Exception($"创建目录失败:{e.Message}");
                        }
                    }

                    if (string.IsNullOrEmpty(FileName))
                    {
                        throw new ArgumentException($"文件名不能为空!");
                    }

                    DataFilePath = Path.Combine(SystemConfig.DataDirectory, FileName + "." + FileType);

                    if (DataFilePath.IndexOfAny(System.IO.Path.GetInvalidPathChars()) >= 0)
                    {
                        //File name is illegal
                        throw new ArgumentException($"文件名不合法:{FileName}");
                    }
                    if (System.IO.File.Exists(DataFilePath))
                    {
                        throw new Exception($"{DataFilePath} 已经存在！");
                    }


                    if (RealTimeSave)
                    {
                        RealTimeCache.Init(SystemConfig.DataDirectory);
                    }

                    //构建数据记录
                    if (MeasureData.DataItems == null)
                    {
                        MeasureData.DataItems = new ObservableCollection<DataItem>();
                    }
                    else
                    {
                        MeasureData.DataItems.Clear();
                    }
                    MeasureData.MeasurementTime = DateTime.Now.ToString("F");
                    MeasureData.MeasurementType = "";
                    MeasureData.MeasurementParameter = VNA.SParameter;
                    MeasureData.DataFormat = "";
                    foreach (var Temp in VNA.SelectedDataFormats)
                    {
                        MeasureData.DataFormat += Temp;
                    }
                    MeasureData.AntennaIdentification = AntennaName + ";" + AntennaModel;
                    MeasureData.PolarizationDirection = PolarizationDirection;
                    MeasureData.Note = Note;

                    //计算频点列表
                    if (VNA.SeMode)
                    {
                        //平均
                        FrequencyList = new ObservableCollection<double>();
                        FrequencyList.Clear();
                        double step = (VNA.GetHZ(VNA.EndFrequency, VNA.EndFrequencyUnit) - VNA.GetHZ(VNA.StartFrequency, VNA.StartFrequencyUnit)) / (VNA.FrequencyCount - 1);
                        for (int i = 0; i < VNA.FrequencyCount; i++)
                        {
                            FrequencyList.Add(VNA.GetHZ(VNA.StartFrequency, VNA.StartFrequencyUnit) + step * i);
                        }
                    }
                    if (VNA.CbMode)
                    {
                        FrequencyList = new ObservableCollection<double>();
                        FrequencyList.Clear();
                        double step = (VNA.GetHZ(VNA.BandFrequency, VNA.BandFrequencyUnit)) / (VNA.FrequencyCount - 1);
                        for (int i = 0; i < VNA.FrequencyCount; i++)
                        {
                            FrequencyList.Add(VNA.GetHZ(VNA.CenterFrequency, VNA.CenterFrequencyUnit) - VNA.GetHZ(VNA.BandFrequency, VNA.BandFrequencyUnit) / 2 + step * i);
                        }
                    }
                    if (VNA.ListMode)
                    {
                        FrequencyList = new ObservableCollection<double>();
                        FrequencyList.Clear();
                        foreach (var temp in VNA.Frequencys.Replace("，", ",").Split(","))
                        {
                            FrequencyList.Add(VNA.GetHZ(double.Parse(Regex.Replace(temp, "[^0-9]", "")), Regex.Replace(temp, "[^a-zA-Z]", "")));

                        }
                    }
                    //添加第一个频点，用于前端的数据绑定
                    DataDictFrequency_PositionCollection.Add(FrequencyList[0], new ObservableCollection<DataItem>());

                    //下面初始化各设备
                    //初始化转台
                    if (!MC.ConnectState)
                    {
                        MC.Connect();
                    }

                    //转台转到起始位置，并确认
                    Axis? Axis = null;
                    //找到转动轴
                    foreach (var axis in MC.Config.Axes)
                    {
                        if (axis.Selected)
                        {
                            Axis = axis;
                            break;
                        }
                    }
                    if (Axis == null)
                    {
                        throw new Exception("未选择转动轴!");
                    }
                    MC.Enable(Axis.AxisID);
                    //运动到起始位置
                    MC.Run(Axis.AxisID, Axis.SetStartPosition, Axis.SetSpeed, 1, 1);
                    double time = Math.Abs((Axis.CurrentPosition - Axis.SetStartPosition) / Axis.SetSpeed) + 20;
                    DateTime st = DateTime.Now;
                    DateTime et = DateTime.Now;
                    //脉冲触发设置
                    if (Axis.EnablePulseOutMode)
                    {
                        MC.TriggerOpen(Axis.AxisID, Axis.PulseTriggerStartPosition, Axis.PulseTriggerEndPosition, Axis.PulseTriggerInterval, 1);
                    }
                    else
                    {
                        MC.TriggerClose(Axis.AxisID);
                    }

                    //添加第一个点，用于绑定前端数据
                    //DataDictPosition_FrequencyCollection.Add(Axis.SetStartPosition, new ObservableCollection<DataItem>());
                    DataDictPosition_FrequencyCollection.Add(0, new ObservableCollection<DataItem>());

                    //初始化矢网，由于初始化矢网的速度很快，与转台初始化同步执行即可，不会占用太长时间,不必使用异步
                    VNA.DisConnect();
                    if (!VNA.ConnectState)
                    {
                        VNA.Connect();
                    }
                    //设置矢网参数

                    //重置
                    VNA.ReSet();
                    VNA.Wait(100);
                    //创建测量
                    //所有测量放在一个通道中，每个轨迹对应一个测量（数据格式），有几个测量就分成几个显示窗口，每个显示窗口显示一条轨迹
                    if (VNA.SelectedDataFormats == null)
                    {
                        throw new Exception("未选择数据格式!");
                    }
                    if (VNA.SelectedDataFormats.Count == 0)
                    {
                        throw new Exception("未选择数据格式!");
                    }
                    foreach (var f in VNA.SelectedDataFormats)
                    {
                        VNA.CreateMeasurement(1, VNA.GetVNAMeasureName(f, VNA.SelectedDataFormats), VNA.SParameter); //创建测量
                        VNA.OpenWindow(int.Parse(VNA.GetVNAMeasureName(f, VNA.SelectedDataFormats)) + 1);
                        VNA.Wait(200);
                        VNA.DisplayMeasurement(int.Parse(VNA.GetVNAMeasureName(f, VNA.SelectedDataFormats)) + 1, int.Parse(VNA.GetVNAMeasureName(f, VNA.SelectedDataFormats)) + 1, VNA.GetVNAMeasureName(f, VNA.SelectedDataFormats)); //显示测量
                        VNA.Wait(200);
                        //设置频点
                        if (VNA.SeMode)
                        {
                            VNA.SetStartFrequency(1, VNA.GetVNAMeasureName(f, VNA.SelectedDataFormats), VNA.GetHZ(VNA.StartFrequency, VNA.StartFrequencyUnit));
                            VNA.Wait(200);
                            VNA.SetEndFrequency(1, VNA.GetVNAMeasureName(f, VNA.SelectedDataFormats), VNA.GetHZ(VNA.EndFrequency, VNA.EndFrequencyUnit));
                            VNA.Wait(200);
                            VNA.SetScanPoints(1, VNA.GetVNAMeasureName(f, VNA.SelectedDataFormats), VNA.FrequencyCount);
                            VNA.Wait(200);
                        }
                        if (VNA.CbMode)
                        {
                            VNA.SetCenterFrequency(1, VNA.GetVNAMeasureName(f, VNA.SelectedDataFormats), VNA.GetHZ(VNA.CenterFrequency, VNA.CenterFrequencyUnit));
                            VNA.Wait(200);
                            VNA.SetFrequencyBandWidth(1, VNA.GetVNAMeasureName(f, VNA.SelectedDataFormats), VNA.GetHZ(VNA.BandFrequency, VNA.BandFrequencyUnit));
                            VNA.Wait(200);
                            VNA.SetScanPoints(1, VNA.GetVNAMeasureName(f, VNA.SelectedDataFormats), VNA.FrequencyCount);
                            VNA.Wait(200);
                        }
                        if (VNA.ListMode)
                        {
                            VNA.Wait(200);
                        }
                        //设置数据格式
                        VNA.SetDataFormat(1, VNA.GetVNAMeasureName(f, VNA.SelectedDataFormats), f);
                        VNA.Wait(200);
                        //设置平滑
                        VNA.SetSmooth(true, 1, VNA.Smooth);
                        VNA.Wait(200);
                        //设置平均
                        VNA.SetAverage(true, 1, VNA.Average);
                        VNA.Wait(200);
                        //设置自动比例
                        VNA.SetYScaleAuto(int.Parse(VNA.GetVNAMeasureName(f, VNA.SelectedDataFormats)) + 1, int.Parse(VNA.GetVNAMeasureName(f, VNA.SelectedDataFormats)) + 1);
                        VNA.Wait(200);
                    }

                    //设置中频带宽
                    VNA.SetIntermediateFrequencyBandwidth(1, VNA.GetHZ(VNA.IFBW, VNA.IFBWUnit));
                    VNA.Wait(200);

                    //关闭矢网发射
                    VNA.OutPut(false);
                    VNA.Wait(200);


                    //设置脉冲触发

                    if (VNA.DeviceInteractEnable)
                    {
                        if (VNA.TriggerInEnable)
                        {
                            //使用触发输入
                            //设置触发源
                            VNA.SetTriggerSource(TriggerSource.外部);
                            VNA.Wait(200);
                            //设置触发信号的类型
                            switch (VNA.SelectedTriggerInSignal)
                            {
                                case "上升沿":
                                    VNA.SetTriggerInput(VNA.SelectedTriggerInPort, TriggerSignalType.上升沿, double.Parse(VNA.SelectedTriggerInSignalWidth), 10);
                                    break;
                                case "下降沿":
                                    VNA.SetTriggerInput(VNA.SelectedTriggerInPort, TriggerSignalType.下降沿, double.Parse(VNA.SelectedTriggerInSignalWidth));
                                    break;
                                case "高电平":
                                    VNA.SetTriggerInput(VNA.SelectedTriggerInPort, TriggerSignalType.高电平, double.Parse(VNA.SelectedTriggerInSignalWidth), 20);
                                    break;
                                case "低电平":
                                    VNA.SetTriggerInput(VNA.SelectedTriggerInPort, TriggerSignalType.低电平, double.Parse(VNA.SelectedTriggerInSignalWidth));
                                    break;
                                default:
                                    throw new Exception();
                            }
                            VNA.Wait(200);

                            //设置触发范围
                            VNA.SetTriggerScope(TriggerScope.ALL);
                            VNA.Wait(200);

                            //设置触发动作
                            switch (VNA.SelectedTriggerInAction)
                            {
                                case "扫描整个通道":
                                    VNA.SetSweepScope(SweepScope.通道);
                                    break;
                                case "扫描一个点":
                                    VNA.SetSweepScope(SweepScope.点);
                                    break;
                                default: throw new Exception();
                            }
                            VNA.Wait(200);
                        }
                        else
                        {
                            //使用总线触发
                            VNA.SetTriggerSource(TriggerSource.总线);
                        }
                        VNA.Wait(200);
                        if (VNA.TriggerOutEnable)
                        {
                            //使能触发输出
                            switch (VNA.SelectedTriggerOutSignal)
                            {
                                case "高电平":
                                    VNA.SetTriggerOut(true, VNA.SelectedTriggerOutPort, 1, VNA.SelectedTriggerOutTime, TriggerSignalType.高电平, double.Parse(VNA.SelectedTriggerOutSignalWidth));
                                    break;
                                case "低电平":
                                    VNA.SetTriggerOut(true, VNA.SelectedTriggerOutPort, 1, VNA.SelectedTriggerOutTime, TriggerSignalType.低电平, double.Parse(VNA.SelectedTriggerOutSignalWidth));
                                    break;
                                case "上升沿":
                                    VNA.SetTriggerOut(true, VNA.SelectedTriggerOutPort, 1, VNA.SelectedTriggerOutTime, TriggerSignalType.上升沿, double.Parse(VNA.SelectedTriggerOutSignalWidth));
                                    break;
                                case "下降沿":
                                    VNA.SetTriggerOut(true, VNA.SelectedTriggerOutPort, 1, VNA.SelectedTriggerOutTime, TriggerSignalType.下降沿, double.Parse(VNA.SelectedTriggerOutSignalWidth));
                                    break;
                                default: throw new Exception();
                            }
                            VNA.Wait(200);
                        }
                        else
                        {
                            //关闭触发输出 
                            VNA.SetTriggerOut(false, VNA.SelectedTriggerOutPort, 1, VNA.SelectedTriggerOutTime, TriggerSignalType.高电平, double.Parse(VNA.SelectedTriggerOutSignalWidth));
                            VNA.Wait(200);
                        }
                    }
                    else
                    {
                        //使用总线触发
                        VNA.SetTriggerSource(TriggerSource.总线);
                        VNA.Wait(200);
                        //关闭触发输出
                        VNA.SetTriggerOut(false, VNA.SelectedTriggerOutPort, 1, VNA.SelectedTriggerOutTime, TriggerSignalType.高电平, double.Parse(VNA.SelectedTriggerOutSignalWidth));
                        VNA.Wait(200);
                    }

                    //获取矢网有无错误
                    string ErrorInfo = "";
                    if (VNA.GetError(ref ErrorInfo) != 0)
                    {
                        throw new Exception($"{VNA.Name}存在错误，请检查！\n{ErrorInfo}\n");
                    }

                    //初始化信号发生器
                    SG.DisConnect();
                    if (!SG.ConnectState)
                    {
                        SG.Connect();
                    }
                    SG.ReSet();
                    SG.Wait(100);
                    //设置频点
                    //设置频点
                    if (SG.SeMode)
                    {
                        SG.SetStartFrequency(SG.GetHZ(SG.StartFrequency, SG.StartFrequencyUnit));
                        SG.Wait(200);
                        SG.SetEndFrequency(SG.GetHZ(SG.EndFrequency, SG.EndFrequencyUnit));
                        SG.Wait(200);
                        SG.SetScanPoints(SG.FrequencyCount);
                        SG.Wait(200);
                    }
                    if (SG.CbMode)
                    {
                        SG.SetStartFrequency(SG.GetHZ(SG.CenterFrequency, SG.CenterFrequencyUnit) - SG.GetHZ(SG.BandFrequency, SG.BandFrequencyUnit) / 2);
                        SG.Wait(200);
                        SG.SetEndFrequency(SG.GetHZ(SG.CenterFrequency, SG.CenterFrequencyUnit) + SG.GetHZ(SG.BandFrequency, SG.BandFrequencyUnit) / 2);
                        SG.Wait(200);
                        SG.SetScanPoints(SG.FrequencyCount);
                        SG.Wait(200);
                    }
                    if (SG.ListMode)
                    {
                        SG.Wait(200);
                    }

                    //设置发射
                    //设置中频带宽
                    SG.SetIntermediateFrequencyBandwidth(SG.GetHZ(SG.IFBW, SG.IFBWUnit));
                    SG.Wait(200);

                    //设置信号发生器发射
                    SG.OutPut(false);
                    if (SG.RadioEnable)
                    {
                        SG.SetOutputPower(SG.OutputPower_dBm);
                    }
                    SG.Wait(200);

                    //设置脉冲触发
                    if (SG.DeviceInteractEnable)
                    {
                        if (SG.TriggerInEnable)
                        {
                            //使用触发输入
                            //设置触发源
                            SG.SetTriggerSource(TriggerSource.外部);
                            SG.Wait(200);
                            //设置触发信号的类型
                            switch (SG.SelectedTriggerInSignal)
                            {
                                case "上升沿":
                                    SG.SetTriggerInput(SG.SelectedTriggerInPort, TriggerSignalType.上升沿, double.Parse(SG.SelectedTriggerInSignalWidth));
                                    break;
                                case "下降沿":
                                    SG.SetTriggerInput(SG.SelectedTriggerInPort, TriggerSignalType.下降沿, double.Parse(SG.SelectedTriggerInSignalWidth));
                                    break;
                                case "高电平":
                                    SG.SetTriggerInput(SG.SelectedTriggerInPort, TriggerSignalType.高电平, double.Parse(SG.SelectedTriggerInSignalWidth));
                                    break;
                                case "低电平":
                                    SG.SetTriggerInput(SG.SelectedTriggerInPort, TriggerSignalType.低电平, double.Parse(SG.SelectedTriggerInSignalWidth));
                                    break;
                                default:
                                    throw new Exception();
                            }
                            SG.Wait(200);

                            //设置触发动作
                            switch (SG.SelectedTriggerInAction)
                            {
                                case "扫描整个通道":
                                    SG.SetSweepScope(SweepScope.通道);
                                    break;
                                case "扫描一个点":
                                    SG.SetSweepScope(SweepScope.点);
                                    break;
                                default: throw new Exception();
                            }
                            SG.Wait(200);
                        }
                        else
                        {
                            //使用总线触发
                            SG.SetTriggerSource(TriggerSource.总线);
                            SG.Wait(200);
                        }

                        if (SG.TriggerOutEnable)
                        {
                            //使能触发输出
                            switch (SG.SelectedTriggerOutSignal)
                            {
                                case "高电平":
                                    SG.SetTriggerOut(true, SG.SelectedTriggerOutPort, 1, SG.SelectedTriggerOutTime, TriggerSignalType.高电平, double.Parse(SG.SelectedTriggerOutSignalWidth));
                                    break;
                                case "低电平":
                                    SG.SetTriggerOut(true, SG.SelectedTriggerOutPort, 1, SG.SelectedTriggerOutTime, TriggerSignalType.低电平, double.Parse(SG.SelectedTriggerOutSignalWidth));
                                    break;
                                case "上升沿":
                                    SG.SetTriggerOut(true, SG.SelectedTriggerOutPort, 1, SG.SelectedTriggerOutTime, TriggerSignalType.上升沿, double.Parse(SG.SelectedTriggerOutSignalWidth));
                                    break;
                                case "下降沿":
                                    SG.SetTriggerOut(true, SG.SelectedTriggerOutPort, 1, SG.SelectedTriggerOutTime, TriggerSignalType.下降沿, double.Parse(SG.SelectedTriggerOutSignalWidth));
                                    break;
                                default: throw new Exception();
                            }
                            SG.Wait(200);
                        }
                        else
                        {
                            //关闭触发输出 
                            SG.SetTriggerOut(false, SG.SelectedTriggerOutPort, 1, SG.SelectedTriggerOutTime, TriggerSignalType.高电平, double.Parse(SG.SelectedTriggerOutSignalWidth));
                            SG.Wait(200);
                        }
                    }
                    else
                    {
                        //使用总线触发
                        SG.SetTriggerSource(TriggerSource.总线);
                        SG.Wait(200);
                        //关闭触发输出
                        SG.SetTriggerOut(false, SG.SelectedTriggerOutPort, 1, SG.SelectedTriggerOutTime, TriggerSignalType.高电平, double.Parse(SG.SelectedTriggerOutSignalWidth));
                        SG.Wait(200);
                    }

                    SG.Wait(200);
                    //获取信号发生器有无错误
                    if (SG.GetError(ref ErrorInfo) != 0)
                    {
                        throw new Exception($"{SG.Name}存在错误，请检查！\n{ErrorInfo}\n");
                    }

                    //等待运动到位
                    while (true)
                    {

                        if (Math.Abs(Axis.CurrentPosition - Axis.SetStartPosition) <= Axis.Accuracy)
                        {
                            break;
                        }
                        et = DateTime.Now;
                        if ((et - st).TotalSeconds >= time)
                        {
                            //很长时间没走到起始位置，报错
                            throw new Exception($"{MC.Name}在{time}秒内未走到起始位置，可能存在故障，请检查！\n");
                        }
                        if (InitBreak)
                        {
                            throw new Exception($"初始化中断！\n");
                        }
                    }
                }
                catch (Exception e)
                {
                    throw new Exception($"测试初始化失败!\n{e.Message}");
                }
            });

            
        }

        public override void MeasurePause()
        {
            try
            {
                ManualResetEvent.Reset();//取消阻塞
            }
            catch (Exception e)
            {
                throw new Exception($"暂停测试操作失败\n{e.Message}");
            }
        }

        public override void MeasureContinue()
        {
            try
            {
                ManualResetEvent.Set();//取消阻塞
            }
            catch (Exception e)
            {
                throw new Exception($"暂停测试操作失败\n{e.Message}");
            }
        }

        public override void MeasureReset()
        {
            throw new NotImplementedException();
        }

        public override async Task MeasureStart(object Type)
        {
            StopFlag = false;
            var SG = DeviceList[2] as ISignalGenerator;
            try
            {
                SG.OutPut(true);
                //开始进行测试
                if (Type == null)
                {
                    throw new Exception($"未知测试类型!");
                }
                if (Type.ToString() == "频谱")
                {
                    MeasureData.MeasurementType = "频谱";
                    SpectrumTest();
                }
                else if (Type.ToString() == "方向图")
                {
                    await Task.Run(() =>
                    {
                        MeasureData.MeasurementType = "方向图";
                        PatternTest();
                    });

                }
                else
                {
                    SG.OutPut(false);
                    throw new Exception($"未知测试类型!\n{Type.ToString()}");
                }
                SG.OutPut(false);
                if (RealTimeSave)
                {
                    DataManage.WriteToFile<MeasureData>(MeasureData, DataFilePath); //保存数据
                }
            }
            catch (Exception e)
            {
                SG.OutPut(false);
                throw new Exception($"测试中断!\n{e.Message}");
            }
        }

        public override void MeasureStop()
        {
            try
            {

                StopFlag = true;
                ManualResetEvent.Set();//取消阻塞
            }
            catch (Exception e)
            {

                throw new Exception($"停止测试操作失败\n{e.Message}");
            }
        }

        public override void SaveData(object obj)
        {
            DataManage.WriteToFile(MeasureData, DataFilePath);
            //MeasureData = new MeasureData();
            //MeasureData.MeasurementTime = DateTime.Now.ToString("F");
            //MeasureData.DataItems = new ObservableCollection<DataItem>();

            //for (int i = -180; i < 181; i++)
            //{
            //    for (int j = 100; j < 501; j = j + 10)
            //    {
            //        DataItem dataItem = new DataItem();
            //        dataItem.Position = i;
            //        dataItem.Frequency = j;
            //        //dataItem.LogarithmicAmplitude = i*i;
            //        //dataItem.Phase = i*i*i+j;
            //        //dataItem.LinearAmplitude = j*i;
            //        //dataItem.RealPart = Math.Sin(i/57.3d) + j / 100;
            //        MeasureData.DataItems.Add(dataItem);
            //    }
            //}

            //DataManage.WriteToFile(MeasureData, "C:\\Users\\Shipengfei\\Desktop\\新建文件夹\\模板.xlsx");

        }

        public void SpectrumTest()
        {
            var MC = DeviceList[0] as IMotionDevice;
            var VNA = DeviceList[1] as IVectorNetworkAnalyzer;
            var SG = DeviceList[2] as ISignalGenerator;
            List<DataItem> Items = new List<DataItem>();
            if (MC == null)
            {
                throw new Exception("未找到运动设备!");
            }
            if (VNA == null)
            {
                throw new Exception("未找到矢量网络分析仪!");
            }
            if (SG == null)
            {
                throw new Exception("未找到信号发生器!");
            }

            double[] Values = new double[FrequencyList.Count];

            //所有测量都扫描
            VNA.MeasureOnce();

            //依次读取每一组测量的数据，
            if (VNA.SelectedDataFormats.Count > 0)
            {
                for (int i = 0; i < VNA.SelectedDataFormats.Count; i++)
                {
                    VNA.ReadData(1, VNA.SelectedDataFormats[i], out Values);
                    Type Type = typeof(DataItem);
                    PropertyInfo[] PropertyInfo = Type.GetProperties();
                    //通过特性值给DataItem赋值
                    PropertyInfo AimProperty = PropertyInfo[0];
                    foreach (PropertyInfo Property in PropertyInfo)
                    {
                        var attr = Property.GetCustomAttribute<DisplayNameAttribute>(false);
                        if (attr == null) continue;
                        if (attr.DisplayName == VNA.SelectedDataFormats[i].Replace(" ", ""))
                        {
                            AimProperty = Property;
                            break;
                        }
                    }
                 
                    DataManage.AddOrUpdateFrequencySpectrumToPositionBaseDict(0, Values.ToList(), FrequencyList.ToList(), (s, v) =>
                    {
                        AimProperty.SetValue(s, v);
                    }, ref DataDictPosition_FrequencyCollection);
                    DataManage.AddOrUpdateFrequencySpectrumToFrequencyBaseDict(0, Values.ToList(), FrequencyList.ToList(), (s, v) =>
                    {
                        AimProperty.SetValue(s, v);
                    }, ref DataDictFrequency_PositionCollection);
                    
                    DataManage.BuildFrequencySpectrumDataItemList(0, FrequencyList.ToList(), Values.ToList(), ref Items, (s, v) =>
                    {
                        AimProperty.SetValue(s, v);
                    });
                }
                if (MeasureData.DataItems == null)
                {
                    MeasureData.DataItems = new ObservableCollection<DataItem>();
                }
                foreach (var v in Items)
                {
                    MeasureData.DataItems.Add(v);
                }
                if (RealTimeSave)
                {
                    RealTimeCache.AddCollection(Items);
                    RealTimeCache.Completed();
                }
            }
        }

        public void PatternTest()
        {
            var VNA = DeviceList[1] as IVectorNetworkAnalyzer;
            var MC = DeviceList[0] as IMotionDevice;
            var SG = DeviceList[2] as ISignalGenerator;

            double Finish = 0;
            double Total = 0;


            if (MC == null)
            {
                throw new Exception("未找到运动设备!");
            }
            if (VNA == null)
            {
                throw new Exception("未找到矢量网络分析仪!");
            }
            if(SG== null)
            {
                throw new Exception("未找到信号发生器!");
            }
            Axis? Axis = null;
            //找到转动轴
            foreach (var axis in MC.Config.Axes)
            {
                if (axis.Selected)
                {
                    Axis = axis;
                    break;
                }
            }
            if (Axis == null)
            {
                throw new Exception("未选择转动轴!");
            }
            Total = (Axis.SetEndPosition - Axis.SetStartPosition) / Axis.SetStepValue + 1;
            if (Axis.StepMove)
            {
                //步进运动
                //读数->->判断是否结束->运动至下一点》读数->->判断是否结束->运动至下一点》》》
                double StepValue = Axis.SetEndPosition > Axis.SetStartPosition ? Axis.SetStepValue : -Axis.SetStepValue;
                double TargetPosition = Axis.SetStartPosition;

                while (true)
                {
                    ManualResetEvent.WaitOne();
                    if (StopFlag) return;

                    //所有测量都扫描
                    VNA.MeasureOnce();
                    VNA.Wait(100);
                    Finish += 1;
                    Process = Finish / Total * 100;
                    double RecordPosition = TargetPosition;
                    //依次读取每一组测量的数据，
                    if (VNA.SelectedDataFormats.Count > 0)
                    {
                        double[] Values = new double[FrequencyList.Count];

                        List<DataItem> Items = new List<DataItem>();
                        for (int i = 0; i < VNA.SelectedDataFormats.Count; i++)
                        {
                            VNA.ReadData(1, VNA.SelectedDataFormats[i], out Values);
                            Type Type = typeof(DataItem);
                            PropertyInfo[] PropertyInfo = Type.GetProperties();
                            //通过特性值给DataItem赋值
                            PropertyInfo AimProperty = PropertyInfo[0];
                            foreach (PropertyInfo Property in PropertyInfo)
                            {
                                var attr = Property.GetCustomAttribute<DisplayNameAttribute>(false);
                                if (attr == null) continue;
                                if (attr.DisplayName == VNA.SelectedDataFormats[i].Replace(" ", ""))
                                {
                                    AimProperty = Property;
                                    break;
                                }
                            }
                            Application.Current.Dispatcher.Invoke(() => 
                            {
                                DataManage.AddOrUpdateFrequencySpectrumToPositionBaseDict(RecordPosition, Values.ToList(), FrequencyList.ToList(), (s, v) =>
                                {
                                    AimProperty.SetValue(s, v);
                                }, ref DataDictPosition_FrequencyCollection);
                                DataManage.AddOrUpdateFrequencySpectrumToFrequencyBaseDict(RecordPosition, Values.ToList(), FrequencyList.ToList(), (s, v) =>
                                {
                                    AimProperty.SetValue(s, v);
                                }, ref DataDictFrequency_PositionCollection);
                            });
                            
                            DataManage.BuildFrequencySpectrumDataItemList(TargetPosition, FrequencyList.ToList(), Values.ToList(), ref Items, (s, v) =>
                            {
                                AimProperty.SetValue(s, v);
                            });
                        }
                        if (MeasureData.DataItems == null)
                        {
                            MeasureData.DataItems = new ObservableCollection<DataItem>();
                        }
                        foreach (var v in Items)
                        {
                            MeasureData.DataItems.Add(v);
                        }
                        if (RealTimeSave)
                        {
                            RealTimeCache.AddCollection(Items);
                        }
                    }

                    //判断是否结束
                    if (Math.Abs(Axis.CurrentPosition - Axis.SetEndPosition) < Math.Abs(Axis.Accuracy * 2))
                    {
                        //测试结束
                        if (RealTimeSave)
                        {
                            RealTimeCache.Completed();
                        }

                        break;
                    }

                    //运动至下一点
                    TargetPosition = TargetPosition + StepValue;
                    if (StepValue > 0)
                    {
                        if (TargetPosition > Axis.SetEndPosition)
                        {
                            TargetPosition = Axis.SetEndPosition;
                        }
                    }
                    else
                    {
                        if (TargetPosition < Axis.SetEndPosition)
                        {
                            TargetPosition = Axis.SetEndPosition;
                        }
                    }

                    MC.Run(Axis.AxisID, TargetPosition, Axis.SetSpeed, 1, 1);
                    //等待达位
                    while (true)
                    {
                        if (Math.Abs(TargetPosition - Axis.CurrentPosition) < Math.Abs(Axis.Accuracy * 2))
                        {
                            break;
                        }
                        if (StopFlag)
                        {
                            throw new Exception("停止测试!");
                        }
                    }
                    if (StopFlag)
                    {
                        throw new Exception("停止测试!");
                    }
                }
            }
            else
            {
                //连续运动测方向图
                //转台转动不能太快，如果转太快，测量不准，或是数据尚未读取，就被新测得的数据覆盖，要计算一些转动速度和矢网的扫描时间
                //触发一次测量后，通过OPC?判断是否测量完成
                //测到一组数据后，要立即进行读取，否则会被覆盖

                //两个测量点的转动时间
                double timea = Math.Abs(Axis.SetStepValue / Axis.SetSpeed);
                //矢网测量一次的时间
                double timeb = 0;
                //从矢网读取数据的时间
                double timec = 0;

                if (timea <= (timeb + timec))
                {
                    //转的太快，应提示减小速度
                }
                if (Axis.SetSpeed * timeb > 1)
                {
                    //测量过程中，转台转的角度很大，应提示减速
                }



                if (!Axis.EnablePulseOutMode)
                {
                    //连续运动，非脉冲触发方式
                    bool AlreadyMove = false;
                    double StepValue = Axis.SetEndPosition > Axis.SetStartPosition ? Axis.SetStepValue : -Axis.SetStepValue;
                    double TargetPosition = Axis.SetStartPosition;

                    while (true)
                    {
                        if (StepValue > 0)
                        {
                            //正向运动
                            if (Axis.CurrentPosition - TargetPosition >= 0)
                            {
                                //所有测量都扫描
                                VNA.MeasureOnce();

                                Finish += 1;
                                Process = Finish / Total * 100;

                                //确保测量完成
                                VNA.Wait(100);
                                double RecordPosition = TargetPosition;

                                //依次读取每一组测量的数据，
                                if (VNA.SelectedDataFormats.Count > 0)
                                {
                                    double[] Values = new double[FrequencyList.Count];

                                    List<DataItem> Items = new List<DataItem>();
                                    for (int i = 0; i < VNA.SelectedDataFormats.Count; i++)
                                    {
                                        VNA.ReadData(1, VNA.SelectedDataFormats[i], out Values);
                                        Type Type = typeof(DataItem);
                                        PropertyInfo[] PropertyInfo = Type.GetProperties();
                                        //通过特性值给DataItem赋值
                                        PropertyInfo AimProperty = PropertyInfo[0];
                                        foreach (PropertyInfo Property in PropertyInfo)
                                        {
                                            var attr = Property.GetCustomAttribute<DisplayNameAttribute>(false);
                                            if (attr == null) continue;
                                            if (attr.DisplayName == VNA.SelectedDataFormats[i].Replace(" ", ""))
                                            {
                                                AimProperty = Property;
                                                break;
                                            }
                                        }
                                        Application.Current.Dispatcher.Invoke(() => 
                                        {
                                            DataManage.AddOrUpdateFrequencySpectrumToPositionBaseDict(RecordPosition, Values.ToList(), FrequencyList.ToList(), (s, v) =>
                                            {
                                                AimProperty.SetValue(s, v);
                                            }, ref DataDictPosition_FrequencyCollection);
                                            DataManage.AddOrUpdateFrequencySpectrumToFrequencyBaseDict(RecordPosition, Values.ToList(), FrequencyList.ToList(), (s, v) =>
                                            {
                                                AimProperty.SetValue(s, v);
                                            }, ref DataDictFrequency_PositionCollection);
                                        });
                                        
                                        DataManage.BuildFrequencySpectrumDataItemList(TargetPosition, FrequencyList.ToList(), Values.ToList(), ref Items, (s, v) =>
                                        {
                                            AimProperty.SetValue(s, v);
                                        });
                                    }
                                    if (MeasureData.DataItems == null)
                                    {
                                        MeasureData.DataItems = new ObservableCollection<DataItem>();
                                    }
                                    foreach (var v in Items)
                                    {
                                        MeasureData.DataItems.Add(v);
                                    }
                                    if (RealTimeSave)
                                    {
                                        RealTimeCache.AddCollection(Items);
                                    }
                                }

                                //判断是否结束
                                if (Math.Abs(Axis.CurrentPosition - Axis.SetEndPosition) < Math.Abs(Axis.Accuracy * 2))
                                {
                                    //测试结束
                                    if (RealTimeSave)
                                    {
                                        RealTimeCache.Completed();
                                    }

                                    break;
                                }

                                //下一点
                                TargetPosition = TargetPosition + StepValue;
                                if (TargetPosition > Axis.SetEndPosition)
                                {
                                    TargetPosition = Axis.SetEndPosition;
                                }
                            }
                        }
                        else
                        {
                            //负向运动
                            if (Axis.CurrentPosition - TargetPosition <= 0)
                            {
                                //所有测量都扫描
                                VNA.MeasureOnce();
                                Finish += 1;
                                Process = Finish / Total * 100;
                                VNA.Wait(100);
                                double RecordPosition = TargetPosition;
                                //依次读取每一组测量的数据，
                                if (VNA.SelectedDataFormats.Count > 0)
                                {
                                    List<DataItem> Items = new List<DataItem>();
                                    for (int i = 0; i < VNA.SelectedDataFormats.Count; i++)
                                    {
                                        double[] Values = new double[FrequencyList.Count];

                                        VNA.ReadData(1, VNA.SelectedDataFormats[i], out Values);
                                        Type Type = typeof(DataItem);
                                        PropertyInfo[] PropertyInfo = Type.GetProperties();
                                        //通过特性值给DataItem赋值
                                        PropertyInfo AimProperty = PropertyInfo[0];
                                        foreach (PropertyInfo Property in PropertyInfo)
                                        {
                                            var attr = Property.GetCustomAttribute<DisplayNameAttribute>(false);
                                            if (attr == null) continue;
                                            if (attr.DisplayName == VNA.SelectedDataFormats[i].Replace(" ", ""))
                                            {
                                                AimProperty = Property;
                                                break;
                                            }
                                        }
                                        Application.Current.Dispatcher.Invoke(() => 
                                        {
                                            DataManage.AddOrUpdateFrequencySpectrumToPositionBaseDict(RecordPosition, Values.ToList(), FrequencyList.ToList(), (s, v) =>
                                            {
                                                AimProperty.SetValue(s, v);
                                            }, ref DataDictPosition_FrequencyCollection);
                                            DataManage.AddOrUpdateFrequencySpectrumToFrequencyBaseDict(RecordPosition, Values.ToList(), FrequencyList.ToList(), (s, v) =>
                                            {
                                                AimProperty.SetValue(s, v);
                                            }, ref DataDictFrequency_PositionCollection);
                                        });
                                        
                                        DataManage.BuildFrequencySpectrumDataItemList(TargetPosition, FrequencyList.ToList(), Values.ToList(), ref Items, (s, v) =>
                                        {
                                            AimProperty.SetValue(s, v);
                                        });
                                    }
                                    if (MeasureData.DataItems == null)
                                    {
                                        MeasureData.DataItems = new ObservableCollection<DataItem>();
                                    }
                                    foreach (var v in Items)
                                    {
                                        MeasureData.DataItems.Add(v);
                                    }
                                    if (RealTimeSave)
                                    {
                                        RealTimeCache.AddCollection(Items);
                                    }
                                }

                                //判断是否结束
                                if (Math.Abs(Axis.CurrentPosition - Axis.SetEndPosition) < Math.Abs(Axis.Accuracy * 2))
                                {
                                    //测试结束
                                    if (RealTimeSave)
                                    {
                                        RealTimeCache.Completed();
                                    }
                                    break;
                                }

                                //下一点
                                TargetPosition = TargetPosition + StepValue;
                                if (TargetPosition < Axis.SetEndPosition)
                                {
                                    TargetPosition = Axis.SetEndPosition;
                                }
                            }
                        }

                        if (!AlreadyMove)
                        {
                            MC.Run(Axis.AxisID, Axis.SetEndPosition, Axis.SetSpeed, 1, 1);
                            AlreadyMove = true;
                        }
                        if (StopFlag)
                        {
                            throw new Exception("停止测试!");
                        }
                    }
                }
                else
                {
                    //连续运动，脉冲触发方式
                    bool AlreadyMove = false;
                    double StepValue = Axis.SetEndPosition > Axis.SetStartPosition ? Axis.SetStepValue : -Axis.SetStepValue;
                    double TargetPosition = Axis.SetStartPosition + StepValue;

                    //启动第一个点
                    VNA.MeasureOnce();
                    Finish += 1;
                    Process = Finish / Total * 100;
                    VNA.Wait(100);
                    double RecordPosition = TargetPosition;
                    //依次读取每一组测量的数据，
                    if (VNA.SelectedDataFormats.Count > 0)
                    {
                        double[] Values = new double[FrequencyList.Count];

                        List<DataItem> Items = new List<DataItem>();
                        for (int i = 0; i < VNA.SelectedDataFormats.Count; i++)
                        {
                            VNA.ReadData(1, VNA.SelectedDataFormats[i], out Values);
                            Type Type = typeof(DataItem);
                            PropertyInfo[] PropertyInfo = Type.GetProperties();
                            //通过特性值给DataItem赋值
                            PropertyInfo AimProperty = PropertyInfo[0];
                            foreach (PropertyInfo Property in PropertyInfo)
                            {
                                var attr = Property.GetCustomAttribute<DisplayNameAttribute>(false);
                                if (attr == null) continue;
                                if (attr.DisplayName == VNA.SelectedDataFormats[i].Replace(" ", ""))
                                {
                                    AimProperty = Property;
                                    break;
                                }
                            }
                            Application.Current.Dispatcher.Invoke(() => 
                            {
                                DataManage.AddOrUpdateFrequencySpectrumToPositionBaseDict(RecordPosition, Values.ToList(), FrequencyList.ToList(), (s, v) =>
                                {
                                    AimProperty.SetValue(s, v);
                                }, ref DataDictPosition_FrequencyCollection);
                                DataManage.AddOrUpdateFrequencySpectrumToFrequencyBaseDict(RecordPosition, Values.ToList(), FrequencyList.ToList(), (s, v) =>
                                {
                                    AimProperty.SetValue(s, v);
                                }, ref DataDictFrequency_PositionCollection);
                            });
                            DataManage.BuildFrequencySpectrumDataItemList(TargetPosition, FrequencyList.ToList(), Values.ToList(), ref Items, (s, v) =>
                            {
                                AimProperty.SetValue(s, v);
                            });
                        }
                        if (MeasureData.DataItems == null)
                        {
                            MeasureData.DataItems = new ObservableCollection<DataItem>();
                        }
                        foreach (var v in Items)
                        {
                            MeasureData.DataItems.Add(v);
                        }
                        if (RealTimeSave)
                        {
                            RealTimeCache.AddCollection(Items);
                        }
                    }

                    //转动转台
                    MC.Run(Axis.AxisID, Axis.SetEndPosition, Axis.SetSpeed, 1, 1);


                    while (true)
                    {
                        if (StepValue > 0)
                        {
                            //正向运动
                            if (Axis.CurrentPosition - TargetPosition >= 0)
                            {
                                //测量，由脉冲信号触发，在这里等待测量完成
                                Finish += 1;
                                Process = Finish / Total * 100;
                                VNA.Wait(100);
                                RecordPosition = TargetPosition;

                                //依次读取每一组测量的数据，
                                if (VNA.SelectedDataFormats.Count > 0)
                                {
                                    double[] Values = new double[FrequencyList.Count];

                                    List<DataItem> Items = new List<DataItem>();
                                    for (int i = 0; i < VNA.SelectedDataFormats.Count; i++)
                                    {
                                        VNA.ReadData(1, VNA.SelectedDataFormats[i], out Values);
                                        Type Type = typeof(DataItem);
                                        PropertyInfo[] PropertyInfo = Type.GetProperties();
                                        //通过特性值给DataItem赋值
                                        PropertyInfo AimProperty = PropertyInfo[0];
                                        foreach (PropertyInfo Property in PropertyInfo)
                                        {
                                            var attr = Property.GetCustomAttribute<DisplayNameAttribute>(false);
                                            if (attr == null) continue;
                                            if (attr.DisplayName == VNA.SelectedDataFormats[i].Replace(" ", ""))
                                            {
                                                AimProperty = Property;
                                                break;
                                            }
                                        }
                                        Application.Current.Dispatcher.Invoke(() => 
                                        {
                                            DataManage.AddOrUpdateFrequencySpectrumToPositionBaseDict(RecordPosition, Values.ToList(), FrequencyList.ToList(), (s, v) =>
                                            {
                                                AimProperty.SetValue(s, v);
                                            }, ref DataDictPosition_FrequencyCollection);
                                            DataManage.AddOrUpdateFrequencySpectrumToFrequencyBaseDict(RecordPosition, Values.ToList(), FrequencyList.ToList(), (s, v) =>
                                            {
                                                AimProperty.SetValue(s, v);
                                            }, ref DataDictFrequency_PositionCollection);
                                        });
                                        DataManage.BuildFrequencySpectrumDataItemList(TargetPosition, FrequencyList.ToList(), Values.ToList(), ref Items, (s, v) =>
                                        {
                                            AimProperty.SetValue(s, v);
                                        });
                                    }
                                    if (MeasureData.DataItems == null)
                                    {
                                        MeasureData.DataItems = new ObservableCollection<DataItem>();
                                    }
                                    foreach (var v in Items)
                                    {
                                        MeasureData.DataItems.Add(v);
                                    }
                                    if (RealTimeSave)
                                    {
                                        RealTimeCache.AddCollection(Items);
                                    }
                                }

                                //判断是否结束
                                if (Math.Abs(Axis.CurrentPosition - Axis.SetEndPosition) < Math.Abs(Axis.Accuracy * 2))
                                {
                                    //测试结束
                                    if (RealTimeSave)
                                    {
                                        RealTimeCache.Completed();
                                    }

                                    break;
                                }

                                //下一点
                                TargetPosition = TargetPosition + StepValue;
                                if (TargetPosition > Axis.SetEndPosition)
                                {
                                    TargetPosition = Axis.SetEndPosition;
                                }
                            }
                        }
                        else
                        {
                            //负向运动
                            if (Axis.CurrentPosition - TargetPosition <= 0)
                            {

                                //测量，由脉冲信号触发，在这里等待测量完成
                                Finish += 1;
                                Process = Finish / Total * 100;
                                VNA.Wait(100);
                                RecordPosition = TargetPosition;
                                //依次读取每一组测量的数据，
                                if (VNA.SelectedDataFormats.Count > 0)
                                {
                                    double[] Values = new double[FrequencyList.Count];

                                    List<DataItem> Items = new List<DataItem>();
                                    for (int i = 0; i < VNA.SelectedDataFormats.Count; i++)
                                    {
                                        VNA.ReadData(1, VNA.SelectedDataFormats[i], out Values);
                                        Type Type = typeof(DataItem);
                                        PropertyInfo[] PropertyInfo = Type.GetProperties();
                                        //通过特性值给DataItem赋值
                                        PropertyInfo AimProperty = PropertyInfo[0];
                                        foreach (PropertyInfo Property in PropertyInfo)
                                        {
                                            var attr = Property.GetCustomAttribute<DisplayNameAttribute>(false);
                                            if (attr == null) continue;
                                            if (attr.DisplayName == VNA.SelectedDataFormats[i].Replace(" ", ""))
                                            {
                                                AimProperty = Property;
                                                break;
                                            }
                                        }
                                        Application.Current.Dispatcher.Invoke(() => 
                                        {
                                            DataManage.AddOrUpdateFrequencySpectrumToPositionBaseDict(RecordPosition, Values.ToList(), FrequencyList.ToList(), (s, v) =>
                                            {
                                                AimProperty.SetValue(s, v);
                                            }, ref DataDictPosition_FrequencyCollection);
                                            DataManage.AddOrUpdateFrequencySpectrumToFrequencyBaseDict(RecordPosition, Values.ToList(), FrequencyList.ToList(), (s, v) =>
                                            {
                                                AimProperty.SetValue(s, v);
                                            }, ref DataDictFrequency_PositionCollection);
                                        });
                                        
                                        DataManage.BuildFrequencySpectrumDataItemList(TargetPosition, FrequencyList.ToList(), Values.ToList(), ref Items, (s, v) =>
                                        {
                                            AimProperty.SetValue(s, v);
                                        });
                                    }
                                    if (MeasureData.DataItems == null)
                                    {
                                        MeasureData.DataItems = new ObservableCollection<DataItem>();
                                    }
                                    foreach (var v in Items)
                                    {
                                        MeasureData.DataItems.Add(v);
                                    }
                                    if (RealTimeSave)
                                    {
                                        RealTimeCache.AddCollection(Items);
                                    }
                                }

                                //判断是否结束
                                if (Math.Abs(Axis.CurrentPosition - Axis.SetEndPosition) < Math.Abs(Axis.Accuracy * 2))
                                {
                                    //测试结束
                                    if (RealTimeSave)
                                    {
                                        RealTimeCache.Completed();
                                    }
                                    break;
                                }

                                //下一点
                                TargetPosition = TargetPosition + StepValue;
                                if (TargetPosition < Axis.SetEndPosition)
                                {
                                    TargetPosition = Axis.SetEndPosition;
                                }
                            }
                        }
                        if (StopFlag)
                        {
                            throw new Exception("停止测试!");
                        }
                    }
                }
            }
        }
    }
}
